import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/building_model.dart';

import '../a_yyt/models/floor_list_entity.dart';
import '../net/response/room_list_bea.dart';

class SelectRoomViewModel extends BaseListViewModel<RoomListBean> {
  SelectRoomViewModel(BuildContext context) : super(context);

  var buildingModel = BuildingModel();

  List<FloorEntity> buildingList = [];
  FloorEntity? selectBuilding;
  List<FloorInfo> floorList = [];
  FloorInfo? selectFloor;

  Future<void> _getBuildingData() async {
    try {
      var list = await buildingModel.getBuildingList();
      if (list != null && list.isNotEmpty) {
        buildingList = list;
        selectBuilding = list[0];
        selectBuilding?.iSelected = true;
        setFloor();
        notifyListeners();
      }
      requestRefresh();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
    }
  }

  void setFloor() {
    if (selectBuilding != null) {
      floorList = selectBuilding?.floorList??[];
      if(floorList.isEmpty||floorList[0].iName!="全部"){
        var first = FloorInfo(
          floorNo: "全部",
        );
        first.iSelected = true;
        floorList.insert(0, first);
        // selectFloor = first;
      }

      selectFloor = floorList[0];

    }
  }

  @override
  void onCreated() {
    super.onCreated();
    requestListData();
  }

  @override
  Future<List<RoomListBean>> requestListData() async {
    showLoadingDialog();
    if (buildingList.isEmpty) {
      await _getBuildingData();
    }
    var list = await buildingModel.getRoomList(
        floorId: selectFloor?.iId ?? "",
        building: selectBuilding?.iId ?? "",
        page: page,
        rows: pageLength);
    hideDialog();
    return list;
  }
}
